<?php
class Message_Model extends CI_Model{
	
	function addMessage($data){
		
		return $this->db->insert('message', $data);
		
	}
	
	function getMessageOf($username){
		
		$this->db->order_by('message_date', 'desc');
		
		return $this->db->get_where('message', array('recipient_username' => $username))->result();
	}
	
	function getMessageById($id){
		
		$message = $this->db->get_where('message', array('message_id' => $id))->result();
		
		if(!$message){
			show_404(base_url());
		}else{
			//update to is read
			$theMessage = $message[0];
			$updateData = array('message_isopened' => 1);

			$this -> db -> update('message', $updateData, array('message_id' => $theMessage->message_id));
			return $theMessage;
		}
	}
	
	function deleteMessage($id){
		
		return $this->db->delete('message', array('message_id' => $id));
		
	}
	
}
?>